//
//  CDTabbarVC.m
//  CustomTabbar
//
//  Created by Dong Chen on 2017/8/31.
//  Copyright © 2017年 Dong Chen. All rights reserved.
//

#import "CDTabbarVC.h"
#import "CDTabBar.h"
#import "NavVC.h"
#import "NavVC.h"
#import "HomeVC.h"
#import "ImageVC.h"
#import "ShowHouseVC.h"
#import "CompanyVC.h"
#import "MineVC.h"

@interface CDTabbarVC ()
@property (nonatomic, strong) HomeVC *homeVC;
@property (nonatomic, strong) ImageVC *imageVC;
@property (nonatomic, strong) CompanyVC *companyVC;
@property (nonatomic, strong) MineVC *mineVC;

@end

@implementation CDTabbarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self InitView];
    [self InitMiddleView];
    // 设置一个自定义 View,大小等于 tabBar 的大小
    UIView *bgView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
    // 给自定义 View 设置颜色
    bgView.backgroundColor = [UIColor whiteColor];
    // 将自定义 View 添加到 tabBar 上
    [self.tabBar insertSubview:bgView atIndex:0];
    
    CGRect rect = CGRectMake(0, 0, kWidth, .5);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,
                                   [UIColor lightGrayColor].CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    　[self.tabBar setShadowImage:img];
    
    　[self.tabBar setBackgroundImage:[[UIImage alloc]init]];
}

- (void)InitMiddleView
{
    CDTabBar *tabBar = [[CDTabBar alloc] init];
    [self setValue:tabBar forKey:@"tabBar"];
        [tabBar setDidMiddBtn:^{
        ShowHouseVC *vc = [[ShowHouseVC alloc] init];
        NavVC *nav = [[NavVC alloc] initWithRootViewController:vc];
        [self presentViewController:nav animated:YES completion:nil];
    }];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSLog(@" --- %@", item.title);
   
}

- (void)InitView
{
    self.view.backgroundColor = [UIColor whiteColor];
    NSArray *titles = @[@"首页", @"效果图",@"装修公司", @"我的"];
    NSArray *images = @[@"icon_home_default", @"icon_design_sketch_default",@"icon_decoration_company_default", @"icon_my_default"];
    NSArray *selectedImages = @[@"icon_home_Selected", @"icon_design_sketch_Selected", @"icon_decoration_company_Selected", @"icon_my_Selected"];
    HomeVC * homeVC = [[HomeVC alloc] init];
    self.homeVC = homeVC;
    ImageVC * imageVC = [[ImageVC alloc] init];
    self.imageVC = imageVC;
    CompanyVC * companyVC = [[CompanyVC alloc] init];
    self.companyVC = companyVC;
    MineVC * mineVC = [[MineVC alloc] init];
    self.mineVC = mineVC;
    NSArray *viewControllers = @[homeVC, imageVC, companyVC, mineVC];
    for (int i = 0; i < viewControllers.count; i++)
    {
        UIViewController *childVc = viewControllers[i];
        [self setVC:childVc title:titles[i] image:images[i] selectedImage:selectedImages[i]];
    }
}

- (void)setVC:(UIViewController *)VC title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    VC.tabBarItem.title = title;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor grayColor];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    NSMutableDictionary *dictSelect = [NSMutableDictionary dictionary];
    dictSelect[NSForegroundColorAttributeName] = [UIColor orangeColor];
    dictSelect[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    [VC.tabBarItem setTitleTextAttributes:dict forState:UIControlStateNormal];
    [VC.tabBarItem setTitleTextAttributes:dictSelect forState:UIControlStateSelected];
    VC.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    VC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    NavVC *nav = [[NavVC alloc] initWithRootViewController:VC];
    [self addChildViewController:nav];
}

@end
